Skip to content

fix(structures): fix fetchMessages pagination and dropped in-memory msgs#201711

Closed
themazim wants to merge 3 commits intowwebjs:mainfrom
themazim:fix/fetch-messages-merge-in-memory
Closed

fix(structures): fix fetchMessages pagination and dropped in-memory msgs#201711
themazim wants to merge 3 commits intowwebjs:mainfrom
themazim:fix/fetch-messages-merge-in-memory

Conversation

@themazim
Copy link
Copy Markdown
Contributor

Description

Follow-up to #201705. Builds on that refactor and fixes two cases where Chat.fetchMessages / Channel.fetchMessages could return fewer messages than requested — in particular, an empty array or a single anchor message where the caller expected up to limit messages.

Changes (both src/structures/Chat.js and src/structures/Channel.js, in the !fromMeFilter && finite fast-path introduced by #201705):

  1. Include in-memory msgs when building merged.
    loaded only contains what msgFindBefore returned from the local IndexedDB message table. Messages that are in the in-memory MsgCollection but not yet persisted (or not returned by the range query) were being dropped. Appending ...msgs into merged preserves them; the existing dedupeByMsgId step guarantees no duplicates.

    Symptom before the fix: chats whose requested window consisted mostly of recent in-memory messages (with nothing older resolvable via msgFindBefore) returned only the anchor message — reproduced and confirmed in the refactor(structures): optimize message loading with msgFindBefore #201705 discussion thread.

  2. Preserve msgs on msgFindBefore status: 404.
    When the anchor is not present in the local DB (NoAnchorMessageErrorstatus: 404), the fast-path previously set msgs = [], throwing away the already-filtered in-memory messages. It now mirrors the !anchorSerialized fallback immediately above: sort by t and slice to limit. Rare in practice (lastReceivedKey is almost always present in the DB), but consistent with how the "cannot run the DB query" case is handled a few lines above.

No public API or type changes.

Related Issue(s)

Builds on #201705.

Testing Summary

Test Details

  • npx eslint src/structures/Chat.js src/structures/Channel.js — clean.
  • Manual verification against a live WhatsApp Web session for both Chat and Channel:
    • chat.fetchMessages({ limit: 50 }) on a chat with a large stored history → returns ~50 messages (regression guard for the single-message bug).
    • chat.fetchMessages({ limit: 50 }) on a chat whose history mostly lives in the in-memory collection → now returns the in-memory messages instead of one or zero.
    • Same two scenarios on a channel via channel.fetchMessages({ limit: 50 }).

Type of Change

  • Dependency change
  • Bug fix
  • New feature
  • Breaking change
  • Non-code change

Checklist

  • My code follows the style guidelines of this project.
  • All new and existing tests pass (npm test). (integration suite requires WWEBJS_TEST_REMOTE_ID; eslint on the touched files is clean)
  • Typings (e.g. index.d.ts) have been updated if necessary. (no public API changes)
  • Usage examples (e.g. example.js) / documentation have been updated if applicable. (not applicable)

Builds on wwebjs#201705 by JuniorRibas. Replaces WAWebChatLoadMessages.loadEarlierMsgs
with WAWebDBMessageFindLocal.msgFindBefore when paginating messages until
reaching searchOptions.limit in both Chat and Channel, resolving models
through MsgStore when the returned entries are not yet serialized.

Follow-up fix: in the !fromMeFilter && finite branch of both
Chat.fetchMessages and Channel.fetchMessages, include the already-loaded
in-memory msgs when constructing merged. The original PR discarded them,
causing chats whose requested window consists mostly of recent in-memory
messages (with nothing older resolvable via msgFindBefore) to return only
the anchor message. dedupeByMsgId keeps the result unique.
When msgFindBefore returns status 404 (anchor message not in local DB),
the fast-path in Chat/Channel.fetchMessages was discarding the
already-filtered in-memory msgs. Mirror the !anchorSerialized fallback
directly above: sort by t and slice to the limit, keeping in-memory
messages rather than returning an empty array.

Also adds short inline comments to the non-obvious decisions introduced
by wwebjs#201705: the msgFindByDirection fallback, the in-memory msgs merge,
and the new 404 fallback — aimed at making upstream review easier.
@github-actions github-actions bot added the api changes API modifications label Apr 12, 2026
@themazim themazim changed the title Fix/fetch messages merge in memory fix(structures): fix fetchMessages pagination and dropped in-memory msgs Apr 12, 2026
@mohamedhossam01
Copy link
Copy Markdown

I had the same issue mentioned here #201706 and followed your solution. Can confirm it's working as expected and I'm able to fetch messages again

@N0N4M3BNS
Copy link
Copy Markdown

It's working perfectly for me. Thanks

@BenyFilho
Copy link
Copy Markdown
Member

What is the difference between #201705 and that one?
Is only that?

let merged = [
     ...loaded,
     ...(anchorMsg ? [anchorMsg] : []),
    // include in-memory msgs so recent (not-yet-persisted) messages aren't dropped
    ...msgs,
];

@themazim
Copy link
Copy Markdown
Contributor Author

What is the difference between #201705 and that one? Is only that?

let merged = [
     ...loaded,
     ...(anchorMsg ? [anchorMsg] : []),
    // include in-memory msgs so recent (not-yet-persisted) messages aren't dropped
    ...msgs,
];

yes, and this:

                                // anchor not in local DB — fall back to in-memory msgs (same as the !anchorSerialized branch above)
                                msgs.sort((a, b) => (a.t > b.t ? 1 : -1));
                                msgs = msgs.slice(
                                    -Math.min(limit, msgs.length),
                                );

The initial PR (#201705) seems to be highly over engineered regarding the initial issue., now that I have dug way deeper into this.

Simple method signature change would probably fix this as well.

@BenyFilho
Copy link
Copy Markdown
Member

If the difference is only that, make your suggestions in the original PR

@BenyFilho BenyFilho closed this Apr 13, 2026
@themazim
Copy link
Copy Markdown
Contributor Author

If the difference is only that, make your suggestions in the original PR

as expected, the signature just changed. #201713

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

api changes API modifications

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants